<?php


namespace app\home\severs;


use think\Exception;
use think\facade\Db;

class OutReceiptSever extends Base
{
    public static function doSure($param,$uid=0)
    {
        $id=$param['id']??0;
        if (!$id){
            return self::jump(1,'参数缺失');
        }
        $info=Db::connect('company')->name('out_receipt')->where('id',$id)->find();
        if (!$info||$info['status']!=1){
            return self::jump(1,'参数有误');
        }
        $mx=Db::connect('company')->name('out_receipt_detail')->where('status',1)->where('out_receipt_id',$id)->select();
        $mx=$mx->toArray()??[];
        if (empty($mx)){
            return self::jump(1,'请添加明细');
        }

        Db::connect('company')->startTrans();
        try {

            if ($info['type']=='销售发货'){
                $gzData=[];
                foreach ($mx as $m){
                    $matInfo=Db::connect('company')->name('material_kucun_view')->where('mdId',$m['material_dossier_id'])
                        ->where('wId',$m['warehouse_id'])
                        ->find();
//                    if ($m['num']>$matInfo['countNum']-$matInfo['sendNum']){
//                        throw new Exception('库存不足');
//                    }
                    $price=Db::connect('company')->name('purveyor_price')
                        ->where('status',1)->where('purveyor_id',$info['punter_id'])
                        ->where('material_dossier_id',$m['material_dossier_id'])
                        ->where('time',date('Y-m'))->order('time DESC')->limit(1)->value('price');
                    $price=$price??0;
                    $gzData[]=[
                        'punter_id'=>$info['punter_id'],
                        'material_dossier_id'=>$m['material_dossier_id'],
                        'create_user'=>$uid,
                        'orderNum'=>orderNum('GZH'),
                        'orderDate'=>date('Y-m-d'),
                        'out_receipt_detail_id'=>$m['id'],
                        'g_orderNum'=>$info['orderNum'],
                        'type'=>$info['type'],
                        'num'=>$m['num'],
                        'price'=>$price,
                        'countPrice'=>$price*$m['num'],
                        'create_time'=>time(),
                        'update_time'=>time()
                    ];
                }
                Db::connect('company')->name('sale_hanging_bill')->insertAll($gzData);
            }
            Db::connect('company')->name('out_receipt')->where('id',$id)->update(['out_user'=>$uid,'status'=>2,'update_time'=>time(),'checkTime'=>time()]);
            Db::connect('company')->name('out_receipt_detail')->where('status','1')->where('out_receipt_id',$id)->update(['status'=>2,'update_time'=>time()]);
            $params=request()->param();
            $params['id']=$id;
            add_log('check', $id, $params,'出库单',$uid);
            Db::connect('company')->commit();
        }catch (\Exception $e){
            Db::connect('company')->rollback();
            return self::jump(1,'操作失败:'.$e->getMessage());
        }
        return self::jump(0,'操作成功');
}

    public static function create($param,$uid=0)
    {
        $printCompany=config('config.companyName');
        Db::connect('company')->StartTrans();
        try {
            $outData = [
                'type' => '其他出库',
                'outTime' => $param['outTime'] ?? date('Y-m-d'),
                'status' => $param['status'] ?? 1,
                'punter_id' => $param['punter_id'] ?? 0,
                'printCompany' => $param['printCompany'] ?? $printCompany[0],
                'orderNum' => $param['orderNum'] ?? '',
                'create_time' => time(),
                'create_user' => $uid,
                'update_time' => time(),
            ];
            $id = Db::connect('company')->name('out_receipt')->insertGetId($outData);
            if (isset($param['wlData'])&&$param['wlData']){

                $mxList=[];
                foreach ($param['wlData'] as $v){
                    $mxList[]=[
                        'out_receipt_id'=>$id,
                        'material_dossier_id'=>$v['mdId'],
                        'num'=>$v['num'],
                        'warehouse_id'=>$v['warehouse_id'],
                        'locator'=>$param['locator']??1,
                        'o_remark'=>$param['o_remark']??'',
                        'create_time'=>time(),
                        'update_time'=>time()
                    ];
                }
                Db::connect('company')->name('out_receipt_detail')->insertAll($mxList);
            }else{
                $mxData=[
                    'out_receipt_id'=>$id,
                    'material_dossier_id'=>$param['mdId'],
                    'num'=>$param['num'],
                    'warehouse_id'=>$param['wId'],
                    'locator'=>$param['locator']??1,
                    'o_remark'=>$param['o_remark']??'',
                    'create_time'=>time(),
                    'update_time'=>time()
                ];
                Db::connect('company')->name('out_receipt_detail')->save($mxData);
            }

            $sures=OutReceiptSever::doSure(['id'=>$id],$uid);
            if ($sures['code']==1){
                throw new Exception($sures['msg']);
            }
            Db::connect('company')->commit();
        }catch (\Exception $e){
            Db::connect('company')->rollback();
            return self::jump(1,$e->getMessage());
        }
        return self::jump();
    }
}